#include "barview.h"
#include <QMouseEvent>
#include <QGraphicsItem>
#include "commondef.h"
#include <QTimeLine>
#include <QGraphicsItemAnimation>
barview::barview(QWidget *parent) :
    QGraphicsView(parent),
    m_bsupportmove(false),
    m_bmove(false)
{
    m_Scene = new QGraphicsScene(this);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setScene(m_Scene);
    m_Scene->setSceneRect(0.0, 0.0, 385.0, 117.0);
    setGeometry(110.0,41.0,385.0,117.0);

    m_ShadowItem = m_Scene->addPixmap(QPixmap(":/image/menu_p.png", "png"));
    m_ShadowItem->setZValue(2);
    m_ShadowItem->setVisible(false);

    m_bgItem = m_Scene->addPixmap(QPixmap(":/image/B_10.png","png"));
    m_bgItem->setZValue(0);
    m_bgItem->setPos(0,0);
}

barview::~barview()
{
    foreach(QGraphicsPixmapItem* paperitem,m_ItemList)
    {
        if(paperitem != NULL)
        {
            delete paperitem;
            paperitem = NULL;
        }
    }
    if(m_Scene != NULL)
    {
        delete m_Scene;
        m_Scene = NULL;
    }
    m_ItemList.clear();
}

void barview::mousePressEvent(QMouseEvent *event)
{
    m_startX = event->pos().x();
    QList<QGraphicsItem *> itemlist = items(event->pos());
    if(itemlist.count() == 2)
    {
        m_selectitem = itemlist.at(0);
        m_ShadowItem->setPos(m_selectitem->pos());
        m_ShadowItem->setVisible(true);
    }
}

void barview::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bsupportmove)
    {
        int imove = event->pos().x() - m_startX;
        if(m_bmove)
        {           
            QGraphicsPixmapItem* firstitem = m_ItemList.first();
            QGraphicsPixmapItem* lastitem = m_ItemList.last();
            if(firstitem->x() > 0 || lastitem->x() < 308 )
            {
                if(firstitem->x() > 0)
                {
                    int i = 0;
                    foreach(QGraphicsPixmapItem* item,m_ItemList)
                    {
                        item->setPos((i++) * MENU_BTN_WIDTH,0);
                    }
                }
                else if(lastitem->x() < 308)
                {
                    foreach(QGraphicsPixmapItem* item,m_ItemList)
                    {
                        item->setX(item->x() + 308 - lastitem->x());
                    }
                }
                m_bmove = false;
            }
            else
            {
                foreach(QGraphicsPixmapItem* item,m_ItemList)
                {
                    item->setX(item->x() + imove);
                }
            }
            m_startX = event->pos().x();
        }
        else
        {
            if(abs(imove) > SHAKE_PARA)
            {
                m_startX = event->pos().x();
                m_bmove = true;
                if(m_ShadowItem->isVisible())
                {
                    m_ShadowItem->setVisible(false);
                }
            }
        }
    }
}

void barview::mouseReleaseEvent(QMouseEvent *event)
{  
    m_ShadowItem->setVisible(false);
    if(m_bmove)
    {
        m_bmove = false;
    }
    else
    {
        QList<QGraphicsItem *> itemlist = items(event->pos());
        if(!itemlist.isEmpty())
        {
            if(m_selectitem == itemlist.at(0))
            {
                emit select(m_ItemList.indexOf((QGraphicsPixmapItem*)m_selectitem));
            }
        }
    }
}

///////////////////////////////PaperBar
PaperBar::PaperBar(QWidget *parent):
        barview(parent)
{
    for(int i = 1; i <= MENU_PAPER_NUM; ++i)
    {
        QString str = QString(":/image/paperbtn/paperbtn (%1).png").arg(i);
        QGraphicsPixmapItem* paperitem = m_Scene->addPixmap(QPixmap(str, "png"));
        paperitem->setPos((i - 1) * MENU_BTN_WIDTH,0);
        paperitem->setZValue(1.0);
        m_ItemList.append(paperitem);
    }
}
/////////////////////////////////////////MenuBar
MenuBar::MenuBar(QWidget *parent):
        barview(parent)
{
    for(int i = 1; i <= MENU_MENU_NUM; ++i)
    {
        QString str = QString(":/image/menubtn/menu_btn (%1).png").arg(i);
        QGraphicsPixmapItem* paperitem = m_Scene->addPixmap(QPixmap(str, "png"));
        paperitem->setPos((i - 1) * MENU_BTN_WIDTH,0);
        paperitem->setZValue(1.0);
        m_ItemList.append(paperitem);
    }
}
//////////////////////////////////////////PaintBar
PaintBar::PaintBar(QWidget *parent):
        barview(parent)
{
    for(int i = 1; i <= PAINT_MENU_NUM;++i)
    {
        QString str = QString(":/image/paintbtn/paintbtn (%1).png").arg(i);
        QGraphicsPixmapItem* paperitem = m_Scene->addPixmap(QPixmap(str, "png"));
        paperitem->setPos((i - 1) * MENU_BTN_WIDTH,0);
        paperitem->setZValue(1.0);
        m_ItemList.append(paperitem);
    }
}
////////////////////////////////////////////CrayonsBar
CrayonsBar::CrayonsBar(QWidget *parent):
        barview(parent)
{
    for(int i = 1; i <= CRAYONS_MENU_NUM;++i)
    {
        QString str = QString(":/image/crayonsbtn/crayonsbtn (%1).png").arg(i);
        QGraphicsPixmapItem* paperitem = m_Scene->addPixmap(QPixmap(str, "png"));
        paperitem->setPos((i - 1) * MENU_BTN_WIDTH,0);
        paperitem->setZValue(1.0);
        m_ItemList.append(paperitem);
    }
}
////////////////////////////////////////////PencilBar
PencilBar::PencilBar(QWidget *parent):
        barview(parent)
{
    for(int i = 1; i <= PENCIL_MENU_NUM;++i)
    {
        QString str = QString(":/image/pencilbtn/pencil (%1).png").arg(i);
        QGraphicsPixmapItem* paperitem = m_Scene->addPixmap(QPixmap(str, "png"));
        paperitem->setPos((i - 1) * MENU_BTN_WIDTH,0);
        paperitem->setZValue(1.0);
        m_ItemList.append(paperitem);
    }
}
/////////////////////////////////////////////StampsBar
StampsBar::StampsBar(QWidget *parent):
        barview(parent)
{
    for(int i = 1; i <= STAMPS_MENU_NUM;++i)
    {
        QString str = QString(":/image/stampsbtn/stampsbtn (%1).png").arg(i);
        QGraphicsPixmapItem* paperitem = m_Scene->addPixmap(QPixmap(str, "png"));
        paperitem->setPos((i - 1) * MENU_BTN_WIDTH,0);
        paperitem->setZValue(1.0);
        m_ItemList.append(paperitem);
    }
}
/////////////////////////////////////////////StickerBar
StickerBar::StickerBar(QWidget *parent):
        barview(parent)
{
    for(int i = 1; i <= STICKERS_MENU_NUM;++i)
    {
        QString str = QString(":/image/stickerbtn/stickerbtn (%1).png").arg(i);
        QGraphicsPixmapItem* paperitem = m_Scene->addPixmap(QPixmap(str, "png"));
        paperitem->setPos((i - 1) * MENU_BTN_WIDTH,0);
        paperitem->setZValue(1.0);
        m_ItemList.append(paperitem);
    }
}
